package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.annotation.RequireLogin;
import cn.wolfcode.luowowo.annotation.UserParam;
import cn.wolfcode.luowowo.domain.*;
import cn.wolfcode.luowowo.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.query.TravelCommentQuery;
import cn.wolfcode.luowowo.query.TravelQuery;
import cn.wolfcode.luowowo.redis.IUserRedisService;
import cn.wolfcode.luowowo.service.*;
import cn.wolfcode.luowowo.util.JsonResult;
import cn.wolfcode.luowowo.util.ParamMap;
import cn.wolfcode.luowowo.util.UMEditorUploader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/travel")
public class TravelController {

    @Autowired
    private ITravelService travelService;
    @Autowired
    private IDestinationService destinationService;
    @GetMapping("/query")
    public Object query(TravelQuery qo){
        qo.setCondition(2);
        Page page = travelService.list(qo);
        return JsonResult.success(page);
    }
    @GetMapping("/input")
    public Object input(String id){
        ParamMap map = new ParamMap();
        if (StringUtils.hasLength(id)){
            Travel travel = travelService.get(id);
            map.put("tv",travel);
        }
        List<Destination> dests = destinationService.list();//dests
        map.put("dests",dests);
        return JsonResult.success(map);
    }

    //上传图片
    @PostMapping("/contentImage")
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception{
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile);
        String callback = request.getParameter("callback");
        String result = "{\"name\":\""+ up.getFileName() +"\", \"originalName\": \""+ up.getOriginalName() +"\", \"size\": "+ up.getSize()
                +", \"state\": \""+ up.getState() +"\", \"type\": \""+ up.getType() +"\", \"url\": \""+ up.getUrl() +"\"}";
        result = result.replaceAll( "\\\\", "\\\\" );
        if(callback == null ){
            return result ;
        }else{
            return "<script>"+ callback +"(" + result + ")</script>";
        }
    }

    @Autowired
    private IUserRedisService userRedisService;
    @RequireLogin
    @PostMapping("/saveOrUpdate")
    //使用参数注入的方法获取用户的信息  贴上注解表示需要用户信息注入了
    public Object saveOrUpdate(Travel travel,@UserParam UserInfo userInfo){
        //2.用户登录信息
        if (userInfo != null){
            travel.setUserId(userInfo.getId());
        }
        String id = travelService.saveOrUpdate(travel);
        return JsonResult.success(id);
    }

    @Autowired
    private IStrategyService strategyService;
    @GetMapping("/detail")
    public Object detail(String id){
        //detail
        Travel travel = travelService.get(id);
        //toasts
        List<Destination> toasts = destinationService.getToasts(travel.getDestId());
        //strategies  阅读为前三的攻略
        List<Strategy> strategies = strategyService.findByDestIdForTravel(travel.getDestId());
        //travels  点击前三的游记
        List<Travel> travels = travelService.findByDestIdForTravel(travel.getDestId());
        //page  分页
        TravelCommentQuery qo = new TravelCommentQuery();
        //设置查询条件是哪个游记的评论
        qo.setTravelId(id);
        Page page = travelCommentService.list(qo);
        ParamMap map = new ParamMap();
        return JsonResult.success(map
                .put("detail",travel)
                .put("toasts",toasts)
                .put("strategies",strategies)
                .put("travels",travels)
                .put("page",page)
        );
    }

    @Autowired
    private ITravelCommentService travelCommentService;
    @PostMapping("/commentAdd")
    @RequireLogin
    public Object commentAdd(TravelComment comment,@UserParam UserInfo userInfo){
        travelCommentService.commentAdd(comment,userInfo);

        TravelCommentQuery qo = new TravelCommentQuery();
        //设置查询条件是哪个游记的评论
        qo.setTravelId(comment.getTravelId());
        Page page = travelCommentService.list(qo);
        return JsonResult.success(page);
    }

    /*问题:每次仅限获取当前登录用户信息的时候,都需要使用request获取token,然后通过token获取当前对象,代码实现太麻烦
    思考:是否可以使用参数注入的方式实现呢
    具体实现方法是:使用springmvc的参数解析器,以前用户参数解析器是用来文件上传的*/
    @GetMapping("/info")
    public Object info(@UserParam UserInfo userInfo){
        return JsonResult.success(userInfo);
    }
    @GetMapping("/currentUser")
    @RequireLogin
    public JsonResult currentUser(HttpServletRequest request){
        String token = request.getHeader("token");
        UserInfo userInfo = userRedisService.getUserByToken(token);
        return JsonResult.success(userInfo);
    }
}














